package com.gxl.common;

import java.lang.reflect.Method;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 能进入servletDispatcher的就可以被拦截
 * @author set_free
 *
 */
public class LoginInterceptor implements HandlerInterceptor{
	
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		System.out.println("LoginInterceptor.preHandle()");
		String requestURI = request.getRequestURI();
		System.out.println(requestURI);
		//判断是登录调操作或到登录页面放行
		if(requestURI.indexOf("/login2") > 0 ||  requestURI.indexOf("/controller_index") > 0){
			return true;
		}else{
			Object users = request.getSession().getAttribute("users");
			if(null == users){
				//未登录
				response.sendRedirect("controller_index.jsp");
				return false;
			}
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		System.out.println("LoginInterceptor.preHandle()");
	}
	//恒执行方法，不管异不异常都会执行，一般可以用在日志记录上
	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		System.out.println("LoginInterceptor.preHandle()");
	}

}
